package 二叉树;

import static java.lang.Math.*;
//import static java.lang.Math.max;

public class LC_110平衡二叉树 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public boolean isBalanced(TreeNode root) {
        if(root==null) return true;
        int result=getHeight(root);
        if(result==0)
            return false;
        else
            return true;
    }
    public static int getHeight(TreeNode node){
        //后序遍历
        if(node==null) return 1;
        //向左递归
        int leftheight=getHeight(node.left);
        if(leftheight==0) return 0;
        //向右递归
        int rightheight=getHeight(node.right);
        if(rightheight==0) return 0;
        //中
        if(abs(leftheight-rightheight)>1)
            return 0;
        else
            return 1+max(leftheight,rightheight);
    }
}
